package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.android.flexbox.FlexDirection;
import com.google.android.flexbox.FlexboxLayoutManager;
import com.google.android.flexbox.JustifyContent;
import com.stark.nettool.lib.NetToolBox;
import com.stark.nettool.lib.TraceRouteInfo;
import com.stark.nettool.lib.TraceRouter;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.RecordAdapter;
import flc.ast.adapter.TraceRouteAdapter;
import flc.ast.databinding.ActivityTracerouteBinding;
import flc.ast.utils.AppSpUtils;
import stark.common.basic.event.EventStatProxy;
import stark.common.bean.StkResBean;

public class TracerouteActivity extends BaseAc<ActivityTracerouteBinding> {

    private String url;
    private RecordAdapter recordAdapter;

    @Override
    protected int onCreate() {
        return R.layout.activity_traceroute;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);

        mDataBinding.ivTracerouteBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.ivUrlClear.setOnClickListener(this);
        mDataBinding.tvStart.setOnClickListener(this);

        FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(this);
        layoutManager.setFlexDirection(FlexDirection.ROW);
        layoutManager.setJustifyContent(JustifyContent.FLEX_START);
        mDataBinding.rvRecordList.setLayoutManager(layoutManager);
        recordAdapter = new RecordAdapter();
        mDataBinding.rvRecordList.setAdapter(recordAdapter);
        recordAdapter.addChildClickViewIds(R.id.ivRecordItemDelete);
        recordAdapter.setOnItemClickListener(this);
        recordAdapter.setOnItemChildClickListener(this);

        mDataBinding.rvTrace.setLayoutManager(new LinearLayoutManager(mContext));
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivUrlClear://网址清空
                mDataBinding.etUrl.setText("");
                break;
            case R.id.tvStart://开始
                url = mDataBinding.etUrl.getText().toString();
                if (TextUtils.isEmpty(url)) {
                    mDataBinding.etUrl.requestFocus();
                    ToastUtils.showShort(getString(R.string.please_input_url));
                    return;
                }
                startTrace();
                saveRecord();
                getRecord();
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        mDataBinding.etUrl.setText(recordAdapter.getItem(position));
    }

    @Override
    public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
        switch (view.getId()) {
            case R.id.ivRecordItemDelete:
                List<String> listSp = AppSpUtils.getTraceRecordList();
                listSp.remove(position);
                AppSpUtils.saveTraceRecordList(listSp);
                getRecord();
        }
    }

    @Override
    protected void initData() {
        getRecord();
    }

    private void startTrace() {

        showDialog(getString(R.string.query_ing));
        TraceRouteAdapter routeAdapter = new TraceRouteAdapter();
        mDataBinding.rvTrace.setAdapter(routeAdapter);
        int ttl = 1;
        int maxTtl = 40;
        NetToolBox.traceRoute(ttl, maxTtl, url, new TraceRouter.IListener() {
            @Override
            public void onGetRoute(TraceRouteInfo container) {
                routeAdapter.addData(container);
            }

            @Override
            public void onTimeOut() {
                dismissDialog();
            }

            @Override
            public void onFinish() {
                dismissDialog();
            }
        });
    }

    private void saveRecord() {
        List<String> listSave = new ArrayList<>();
        listSave.add(url);
        List<String> listSp = AppSpUtils.getTraceRecordList();
        if (listSp != null && listSp.size() > 0) {
            listSp.remove(url);
            listSp.add(0, url);
            AppSpUtils.saveTraceRecordList(listSp);
        } else {
            AppSpUtils.saveTraceRecordList(listSave);
        }
    }

    private void getRecord() {
        List<String> listSp = AppSpUtils.getTraceRecordList();
        if (listSp != null && listSp.size() > 0) {
            recordAdapter.setList(listSp);
            mDataBinding.rvRecordList.setVisibility(View.VISIBLE);
        } else {
            mDataBinding.rvRecordList.setVisibility(View.GONE);
        }
    }

}